import os
from datetime import datetime
import json
# from .models import DiagnosisHistory

# 获取上一级目录
current_directory = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# 构建文件路径(注意热插拔，要把每个app的static和templates文件夹都放在自身app下，而其他通用放在大的static和templates文件夹下)
disease_json_file_path = os.path.join(
    current_directory, "static", "json", "disease.json"
)
class DiseaseResponse:
    """疾病诊断响应数据模板类"""
    @staticmethod
    def create_response(diagnosis_result, prediction_rate, introduction="", img_url="", treatment="",diagnosis_date=datetime.now().strftime("%Y-%m-%d %H:%M:%S")):
        """ 
        return dict with the given parameters.
        Parameters:
            diagnosis_result (str): The result of the diagnosis.
            prediction_rate (dict): The confidence level of the prediction.
            introduction (str, optional): A brief introduction to the disease. Defaults to an empty string.
            img_url (str, optional): The URL of an image related to the disease. Defaults to an empty string.
            treatment (str, optional): Recommended treatment for the disease. Defaults to an empty string.
        """
        return {
            "diagnosis_result": diagnosis_result,
            "prediction_rate": prediction_rate,
            "introduction": introduction,
            "img_url": img_url,
            "treatment": treatment,
            "disease_name": ["健康", "哮喘", "新冠肺炎", "常见感冒"],
            "diagnosis_date": diagnosis_date
        }
    
    @staticmethod
    def create_from_diagnosis_history(diagnosis):
        """从DiagnosisHistory对象创建响应"""
        with open(disease_json_file_path, "r", encoding="utf-8") as f:
            disease_data = json.load(f)
        
        # return DiseaseResponse.create_response(
        #     diagnosis_result=diagnosis.get('diagnosis_result',""),
        #     prediction_rate=diagnosis.get('prediction_rates', []), # prediction_rates,
        #     introduction=disease_data.get(diagnosis.get('diagnosis_result', ""), {}).get("introduction", ""),
        #     img_url=disease_data.get(diagnosis.get('diagnosis_result', ""), {}).get("img_url", ""),
        #     treatment=disease_data.get(diagnosis.get('diagnosis_result', ""), {}).get("treatment", ""),
        #     diagnosis_date=diagnosis.get('diagnosis_date',"").strftime("%Y-%m-%d %H:%M:%S")
        # )
        return DiseaseResponse.create_response(
            diagnosis_result=diagnosis.diagnosis_result,
            prediction_rate=diagnosis.prediction_rates,
            introduction=disease_data.get(diagnosis.diagnosis_result, {}).get("introduction", ""),
            img_url=disease_data.get(diagnosis.diagnosis_result, {}).get("img_url", ""),
            treatment=disease_data.get(diagnosis.diagnosis_result, {}).get("treatment", ""),
            diagnosis_date=diagnosis.diagnosis_date.strftime("%Y-%m-%d %H:%M:%S")
        )
    
    @staticmethod
    def create_other_disease(prediction_rate):
        """创建'其他疾病'的标准响应"""
        return DiseaseResponse.create_response(
            diagnosis_result="可能存在其他呼吸道疾病",
            prediction_rate=prediction_rate,
            introduction="建议做好防护措施，可能存在其他呼吸道疾病。",
            treatment="请咨询医生进行进一步检查和治疗。",
            img_url="https://ai-public.mastergo.com/ai/img_res/497c0eeccb67c901771a309a6bcd81b6.jpg"
        )
